package com.zhihu_lvliangi_android.option.general;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.option.parent.ParentActivity;

public class GeneralWebviewActivity extends ParentActivity implements OnClickListener{

	public static final String URL_KEY = "url_key";

	private String mUrl ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_general_webview;
	}

	@Override
	protected void setupViews() {
		findById();
		initView();
		initActionBar();
	}

	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar));
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setIcon(new BitmapDrawable());//不设置图标icon
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void initialized() {
		setListener();
	}

	@Override
	protected void threadTask() {

	}


	/** 后退 */
	private ImageButton back;
	/** 前进 */
	private ImageButton forward;
	/** 刷新 */
	private ImageButton refresh;
	/** 进度条 */
	private ProgressBar progressBar;
	/** 工具栏顶部横线 */
	private View toolbarline;
	/** 按钮标志位，代表按下哪个按钮 */
	private String btFlag = null;
	/** 是否点击刷新按钮 */
	private boolean isRefersh = false;
	private RelativeLayout webviewBottom;
	/** 网页 */
	private WebView webView;
	private WebSettings settings;






	/**
	 * 初始化界面
	 */
	private void initView() {
		//webView.loadUrl("http://www.baidu.com");
		mUrl = getIntent().getStringExtra(URL_KEY);
		System.out.println(mUrl);
		webView.loadUrl(mUrl);

	}


	/**
	 * 设置监听
	 */
	private void setListener() {
		back.setOnClickListener(this);
		forward.setOnClickListener(this);
		refresh.setOnClickListener(this);
	}


	/**
	 * 初始化ui
	 */
	@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
	private void findById() {
		back = (ImageButton) findViewById(R.id.back);
		forward = (ImageButton) findViewById(R.id.forward);
		refresh = (ImageButton) findViewById(R.id.refresh);
		toolbarline = findViewById(R.id.toolbarline);
		webviewBottom = (RelativeLayout) findViewById(R.id.webviewBottom);
		progressBar = (ProgressBar) findViewById(R.id.progressbar);
		webView = (WebView) findViewById(R.id.webview);

		settings = webView.getSettings();
		// 设置WebView属性，能够执行Javascript脚本
		settings.setJavaScriptEnabled(true);
		// js与android调用接口
		webView.addJavascriptInterface(new MyJavascriptInterface(), "android");
		// 设置Web视图
		webView.setWebViewClient(new MyWebViewClient());
		// 处理Javascript的对话框、网站图标、网站title、加载进度等
		webView.setWebChromeClient(new MyWebChromeClient());
	}


	class MyJavascriptInterface {
		public MyJavascriptInterface() {


		}


		/**
		 * 工具栏显示
		 */
		public void setVisibility() {
			toolbarline.setVisibility(View.VISIBLE);
			webviewBottom.setVisibility(View.VISIBLE);
		}


		/**
		 * 工具栏消失
		 */
		public void setGone() {
			toolbarline.setVisibility(View.GONE);
			webviewBottom.setVisibility(View.GONE);
		}
	}


	class MyWebChromeClient extends WebChromeClient {


		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);
			setTitle(title);
		}


		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress == 100) {
				progressBar.setProgress(newProgress);
				progressBar.setVisibility(View.GONE);
				isRefersh = false;
				refresh.setImageDrawable(getResources().getDrawable(
						R.drawable.mini_webview_refresh));
			} else {
				if (progressBar.getVisibility() == View.GONE)
					progressBar.setVisibility(View.VISIBLE);
				progressBar.setProgress(newProgress);
			}


			super.onProgressChanged(view, newProgress);
		}
	}


	class MyWebViewClient extends WebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			if ("refresh".equals(btFlag)) {// 判断是否是刷新按钮
				if (isRefersh) {
					refresh.setImageDrawable(getResources().getDrawable(
							R.drawable.mini_webview_refresh_click));
				}
			}


		}


		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			if (webView.canGoBack()) {// 如果能后退，则更新图像状态
				back.setImageDrawable(getResources().getDrawable(
						R.drawable.mini_webview_back));
			} else {
				back.setImageDrawable(getResources().getDrawable(
						R.drawable.mini_webview_back_disable));
			}
			if (webView.canGoForward()) {// 如果能前期，则更新图像状态
				forward.setImageDrawable(getResources().getDrawable(
						R.drawable.mini_webview_forward));
			} else {
				forward.setImageDrawable(getResources().getDrawable(
						R.drawable.mini_webview_forward_disable));
			}
		}


		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:// 后退
			btFlag = "back";// 代表按下的是后退按钮
			if (webView.canGoBack()) {// 如果能返回
				webView.goBack();
			}
			break;
		case R.id.forward:// 前进
			btFlag = "forward";// 代表按下的是前进按钮
			if (webView.canGoForward()) {// 如果能前进
				webView.goForward();
			}
			break;
		case R.id.refresh:// 刷新
			btFlag = "refresh";// 代表按下的是刷新按钮
			if (isRefersh) {// 当正在刷新中，再次点击刷新按钮时，刷新结束
				isRefersh = false;
				// webview停止加载
				webView.stopLoading();
				refresh.setImageDrawable(getResources().getDrawable(
						R.drawable.mini_webview_refresh));
			} else {
				isRefersh = true;
				webView.reload();
			}
			break;


		default:
			break;
		}
	}


	@Override
	public boolean dispatchKeyEvent( KeyEvent event) {
		//onKeyDown 与parentActivity 的dispatchKeyEvent 冲突
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP && webView.canGoBack()) {
			webView.goBack();
			return true;
		}else {
			return super.dispatchKeyEvent(event);
		}

	}

	//@Override
	//public void onBackPressed() {
	//	showShortToast("onBackPressed =="+webView.canGoBack());
	//	if (webView.canGoBack()) {
	//		btFlag = "back";
	//		webView.goBack();
	//	}else {
	//		finish();
	//	}
	//}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		webView.clearHistory();
	}
}
